package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.vo.CustomerVo;
import com.ruoyi.system.domain.zhuangxiumain.vo.PasswordVo;
import com.ruoyi.system.mapper.vant.customer.ApiCustomerVoMapper;
import com.ruoyi.system.service.vantservice.customerervice.ApiCustomerVoService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by 小董 on 2021/3/10 16:08
 */
@Service
public class ApiCustomerVoServiceImpl implements ApiCustomerVoService {
    @Autowired
    private ApiCustomerVoMapper apiCustomerVoMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询用户信息
     * @return
     */
    @Override
    public CustomerVo selectCustomerVo() {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        CustomerVo customerVo = new CustomerVo();
        customerVo.setCustomerId(customerLogin.getCustomerId());
        return apiCustomerVoMapper.selectCustomerVo(customerVo);
    }

    /**
     * 修改用户性别
     * @param customerVo
     * @return
     */
    @Override
    public int updateCustomerVoOne(CustomerVo customerVo) {
        int row = 0;
        row = apiCustomerVoMapper.updateCustomerCi(customerVo);
        return row;
    }

    /**
     * 注销用户信息
     * @param customerVo
     * @return
     */
    @Override
    public int deleteCustomerVoOne(CustomerVo customerVo) {
        int row = 0;
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        customerVo.setCustomerId(customerLogin.getCustomerId());
        if(customerLogin.getPassword().equals(Md5Utils.hash(customerVo.getPassword()))){
            row = apiCustomerVoMapper.deleteCustomerCl(customerVo);
            if(row == 1){
                row = apiCustomerVoMapper.deleteCustomerCi(customerVo);
            }
        }else {
            //密码不一致
            row = 2;
            return row;
        }
        return row;
    }

    /**
     * 修改密码
     * @param passwordVo
     * @return
     */
    @Override
    public int updatePassword(PasswordVo passwordVo) {
        int row = 0;
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        if(customerLogin.getPassword().equals(Md5Utils.hash(passwordVo.getPassword1()))){
            CustomerVo customerVo = new CustomerVo();
            customerVo.setCustomerId(customerLogin.getCustomerId());
            customerVo.setPassword(Md5Utils.hash(passwordVo.getPassword2()));
            row = apiCustomerVoMapper.updateCustomerCl(customerVo);
        }else {
            row = 2;
            return row;
        }
        return row;
    }


}
